package com.pet.server.ext.action.user;

import java.util.Date;

import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.PetInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.DateUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Petupdate extends ActionBase{
	
	public String pet_cate,pet_name,breed,ico_path,sex;
	public Date birthday;
	public Integer id;
	
	public Petupdate(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		pet_cate = params.getFieldValue("pet_cate");
		pet_name = params.getFieldValue("pet_name");
		breed = params.getFieldValue("breed");
		ico_path = params.getFileUrl("ico_path");
		birthday = DateUtil.getDate(NumberUtil.getLong(params.getFieldValue("birthday"), 0l) );
		sex = params.getFieldValue("sex");//male female
		id = NumberUtil.getInteger(params.getFieldValue("pet_id"));
		
		System.out.println(id);
		
		if(StringUtil.isEmpty(id,pet_cate,pet_name,breed,birthday,sex)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		JSONObject data = new JSONObject();
		PetInfo pet = userService.findPetInfoById(id);
		if(null==pet || !StringUtil.equals(user.user_id, pet.getUser_id())){
			throw ApiError.Type.EXIST_PARAM.toException();
		}
		System.out.println(pet.pet_id);
		pet.birthday=birthday;
		pet.pet_cate=pet_cate;
		pet.breed=breed;
		pet.ico_path=ico_path;
		pet.pet_name=pet_name;
		pet.sex=sex;
		userService.updatePetInfoById(pet);
		Result result = new Result();
		data.accumulate("message", "编辑成功!");
		result.put("data", data);
	    return result;
	}
}
